using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.DirectX.DirectDraw;
using System.Drawing;
using System.Windows.Forms;

namespace TowerDefense_Dx.Core
{
    public class DxInitGraphics
    {
        protected Control _target = null;
        protected Device _graphicsDevice = null;
        protected Clipper _graphicsClipper = null;
        protected Surface _primarySurface = null;
        protected Surface _backBuffer = null;
        public Device GraphicsDevice
        {
            get
            {
                return _graphicsDevice;
            }
        }
        public Surface BackBuffer
        {
            get
            {
                return _backBuffer;
            }
        }
        public DxInitGraphics(Control renderControl)
        {
            this._target = renderControl;
            _graphicsDevice = new Device();
#if DEBUG
            _graphicsDevice.SetCooperativeLevel(this._target, CooperativeLevelFlags.Normal);            
#else
            _graphicsDevice.SetCooperativeLevel(this._target, CooperativeLevelFlags.FullscreenExclusive);
            _graphicsDevice.SetDisplayMode(640,480,32,60,true);
#endif
            this.CreateSurface();
        }

        public void CreateSurface()
        {
            SurfaceDescription desc = new SurfaceDescription();
            desc.SurfaceCaps.PrimarySurface = true;
#if !DEBUG
            desc.SurfaceCaps.Flip = true;
            desc.SurfaceCaps.Complex = true;
            desc.BackBufferCount = 1;
#endif
            _primarySurface = new Surface(desc, _graphicsDevice);
            desc.Clear();
#if DEBUG
            desc.Width = _primarySurface.SurfaceDescription.Width;
            desc.Height = _primarySurface.SurfaceDescription.Height;
            desc.SurfaceCaps.OffScreenPlain = true;
            _backBuffer = new Surface(desc,_graphicsDevice);
#else
            desc.SurfaceCaps.BackBuffer = true;
            _backBuffer = _primarySurface.GetAttachedSurface(desc.SurfaceCaps);
#endif
            _graphicsClipper = new Clipper(_graphicsDevice);
            _graphicsClipper.Window = _target;
            _primarySurface.Clipper = this._graphicsClipper;
        }

        public void Flip()
        {
            if (!this._target.Created)
            {
                return;
            }
            if (_primarySurface == null || _backBuffer == null)
            {
                return;
            }
            try
            {
#if DEBUG
                _primarySurface.Draw(_backBuffer, DrawFlags.Wait);                
#else
                _primarySurface.Flip(_backBuffer, FlipFlags.Wait);
#endif
            }
            catch (SurfaceLostException)
            {
                this.CreateSurface();
            }
        }
        public void Clear()
        {
            _backBuffer.ColorFill(Color.White);
            
        }

    }
}
